#include "ParseWMV.h"

int parse_wmv(char* filePath,struct Meta* ret_m)
{
	FILE* fp;
	

	unsigned char HEADER_OBJECT_GUID[] 	= {0x30,0x26,0xB2,0x75,0x8E,0x66,0xCF,0x11,0xA6,0xD9,0x00,0xAA,0x00,0x62,0xCE,0x6C };
	unsigned char FILE_PROPERTIES_OBJECT_GUID[]={0xA1,0xDC,0xAB,0x8C,0x47,0xA9,0xCF,0x11,0x8E,0xE4,0x00,0xC0,0x0C,0x20,0x53,0x65 };
	unsigned char DATA_OBJECT_GUID[] 	= {0x36,0x26,0xB2,0x75,0x8E,0x66,0xCF,0x11,0xA6,0xD9,0x00,0xAA,0x00,0x62,0xCE,0x6C };
	unsigned char CODEC_LIST_OBJECT_GUID[] 	= {0x40,0x52,0xD1,0x86,0x1D,0x31,0xD0,0x11,0xA3,0xA4,0x00,0xA0,0xC9,0x03,0x48,0xF6 };
	unsigned char STREAM_PROPERTIES_OBJECT_GUID[] 	= {0x91,0x07,0xDC,0xB7,0xB7,0xA9,0xCF,0x11,0x8E,0xE6,0x00,0xC0,0x0C,0x20,0x53,0x65 };
	unsigned char VIDEO_MEDIA_GUID[] = {0xC0,0xEF,0x19,0xBC,0x4D,0x5B,0xCF,0x11,0xA8,0xFD,0x00,0x80,0x5F,0x5C,0x44,0x2B };
	unsigned char AUDIO_MEDIA_GUID[] = {0x40,0x9E,0x69,0xF8,0x4D,0x5B,0xCF,0x11,0xA8,0xFD,0x00,0x80,0x5F,0x5C,0x44,0x2B };
	unsigned char CONTENT_DESCRIPTION_GUID[] = {0x33,0x26,0xB2,0x75,0x8E,0x66,0xCF,0x11,0xA6,0xD9,0x00,0xAA,0x00,0x62,0xCE,0x6C };

	unsigned char ObjectID[16];
	unsigned char ObjectSize[8];
	

	unsigned char titleLength[2];
	unsigned char authorLength[2];
	unsigned char copyrightLength[2];
	unsigned char descriptionLength[2];
	unsigned char ratingLength[2];

	unsigned char* title;
	unsigned char* author;
	unsigned char* description;
	unsigned char* copyright;
	unsigned char* rating;

	unsigned char streamType[16];
	unsigned char errorCorrection[16];
	unsigned char timeOffset[8];
	unsigned char typeSpecifiedLength[4];
	unsigned char errorCorrectionLength[4];
	unsigned char Flags[2];
	unsigned char Reserved[4];
	
	unsigned char Width[4];
	unsigned char Height[4];

	int size;

	int i = 0;

	if(fp == NULL){
		printf("FAILED TO OPEN FILE");
		return 0;
	}


	ret_m->fileSize = GetFileLength(fp);

	ret_m->upnpClass=(char*)malloc(strlen(UPNP_VIDEO)+1);
	strcpy(ret_m->upnpClass,UPNP_VIDEO);

	ret_m->ProtocolInfo = (char*)malloc(strlen(PROTOCOL_WMV)+1);
	strcpy(ret_m->ProtocolInfo,PROTOCOL_WMV);





	while(i < 5)
	{
	
		fread(ObjectID, sizeof(unsigned char), (size_t)16,fp);
		fread(ObjectSize, sizeof(unsigned char), (size_t)8,fp);
		size = getIntFrom4Byte(ObjectSize);

		if(strncmp(ObjectID,HEADER_OBJECT_GUID,16) == 0 )
		{
			fseek(fp,6,SEEK_CUR);
		}
		else if(strncmp(ObjectID,CONTENT_DESCRIPTION_GUID,16) == 0)
		{
			fread(titleLength, sizeof(unsigned char),(size_t)2,fp);
			fread(authorLength, sizeof(unsigned char),(size_t)2,fp);
			fread(copyrightLength, sizeof(unsigned char),(size_t)2,fp);
			fread(descriptionLength, sizeof(unsigned char),(size_t)2,fp);
			fread(ratingLength, sizeof(unsigned char),(size_t)2,fp);

			title = readChars(getIntFromByte(titleLength,2),fp);
			author = readChars(getIntFromByte(authorLength,2),fp);
			copyright = readChars(getIntFromByte(copyrightLength,2),fp);
			description = readChars(getIntFromByte(descriptionLength,2),fp);
			rating = readChars(getIntFromByte(ratingLength,2),fp);


		}
		else if(strncmp(ObjectID,STREAM_PROPERTIES_OBJECT_GUID,16) == 0)
		{
			fread(streamType, sizeof(unsigned char), (size_t)16,fp);

			if(strncmp(streamType,VIDEO_MEDIA_GUID,16))
			{ 
				fread(errorCorrection,sizeof(unsigned char), (size_t)16,fp);
        		fread(timeOffset,sizeof(unsigned char), (size_t)8,fp);
        		fread(typeSpecifiedLength,sizeof(unsigned char), (size_t)4,fp);
        		fread(errorCorrectionLength,sizeof(unsigned char), (size_t)4,fp);
        		fread(Flags,sizeof(unsigned char), (size_t)2,fp);
        		fread(Reserved,sizeof(unsigned char), (size_t)4,fp);
        		fread(timeOffset,sizeof(unsigned char), (size_t)8,fp);

				fread(Width,sizeof(unsigned char),(size_t)4,fp);
				fread(Height,sizeof(unsigned char),(size_t)4,fp);
				fseek(fp,size-11,SEEK_CUR);

			} else if(strncmp(streamType,AUDIO_MEDIA_GUID,16))
			{
				fseek(fp,size-40,SEEK_CUR);
			}else
				fseek(fp,size-40,SEEK_CUR);

		}else
			fseek(fp,size-24,SEEK_CUR);
		

	}
	
	

	fclose(fp);
	return FILE_MOVIE;
}
